/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import Negocio.Empresa;
import Negocio.Insumo;
import Negocio.Proveedor;
import Negocio.TipoDeInsumo;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego
 */
public class FrmInsumo extends javax.swing.JDialog {
    private Empresa unaEmpresa;
    private TipoDeInsumo unTipoDeInsumo;
    private Proveedor unProveedor;
    private Insumo unInsumo;
     private DefaultListModel modelInsumo = new DefaultListModel();
    /**
     * Creates new form FrmInsumo
     */
    public FrmInsumo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    public FrmInsumo(java.awt.Frame parent, boolean modal,Empresa unaEmpresa) {
        super(parent, modal);
        initComponents();
        this.unaEmpresa = unaEmpresa;
        this.inicializar();
    }
    public void limpiarCampos(){
        this.cargarInsumos();
        this.unInsumo = null;
        this.unTipoDeInsumo = null;
        this.unProveedor = null;
        this.TxtCantidad.setText("");
        this.TxtProveedor.setText("");
        this.TxtTipoDeInsumo.setText("");
        
    }
    public void cargarInsumos(){
        this.modelInsumo.clear();
        if(this.unaEmpresa.getInsumos() != null && this.unaEmpresa.getInsumos().size() > 0){
        Iterator it = this.unaEmpresa.getInsumos().iterator();
        while(it.hasNext()){
            this.modelInsumo.addElement((Insumo) it.next());
        }
        }else{
            this.modelInsumo.addElement("No se Encontraron Insumos");
        }
    }
    
    public void setTipoDeInsumo(TipoDeInsumo unTipoDeInsumo){
        this.unTipoDeInsumo = unTipoDeInsumo;
        this.TxtTipoDeInsumo.setText(this.unTipoDeInsumo.toString());
               
    }
    public void setProveedor(Proveedor unProveedor){
        this.unProveedor = unProveedor;
        this.TxtProveedor.setText(this.unProveedor.toString());
        this.findTipoDeInsumo.setEnabled(true);
    }
    
    public void  inicializar(){
        this.lstInsumos.setModel(modelInsumo);
        this.cargarInsumos();
        this.limpiarCampos();
        this.TxtTipoDeInsumo.setEditable(false);
        this.TxtProveedor.setEditable(false);
        
    }
    
    public void ponerCampos(){
       this.unTipoDeInsumo = this.unInsumo.getTipoDeInsumo();
       this.TxtTipoDeInsumo.setText(this.unTipoDeInsumo.toString());
       this.TxtTipoDeInsumo.setEditable(false);
       this.BtnFindProveedor.setEnabled(false);
       this.TxtProveedor.setText("");
       this.TxtProveedor.setEditable(false);
       this.unProveedor = null;
       this.TxtProveedor.setText("");
       this.TxtTipoDeInsumo.setEditable(false);
       this.TxtProveedor.setEditable(false);
       this.TxtCantidad.setText(this.unInsumo.getCantidad().toString());       
       this.BtnNuevo.setEnabled(false);
       this.BtnAceptar.setEnabled(true);
       this.BtnCancelar.setEnabled(true);
       
    }
    public void desactivarBotones(){
       this.BtnNuevo.setEnabled(true);
       this.BtnAceptar.setEnabled(false);
       this.BtnCancelar.setEnabled(false);
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        TxtTipoDeInsumo = new javax.swing.JTextField();
        findTipoDeInsumo = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        BtnCancelar = new javax.swing.JButton();
        BtnAceptar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        TxtProveedor = new javax.swing.JTextField();
        BtnFindProveedor = new javax.swing.JToggleButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstInsumos = new javax.swing.JList();
        BtnNuevo = new javax.swing.JButton();
        BtnClose = new javax.swing.JButton();
        BtnBorrar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        TxtCantidad = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Tipo de Insumo");

        findTipoDeInsumo.setText("...");
        findTipoDeInsumo.setEnabled(false);
        findTipoDeInsumo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findTipoDeInsumoActionPerformed(evt);
            }
        });

        jLabel2.setText("Cantidad");

        BtnCancelar.setText("Cancelar");
        BtnCancelar.setEnabled(false);
        BtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCancelarActionPerformed(evt);
            }
        });

        BtnAceptar.setText("Guardar");
        BtnAceptar.setEnabled(false);
        BtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAceptarActionPerformed(evt);
            }
        });

        jLabel4.setText("Proveedor");

        BtnFindProveedor.setText("...");
        BtnFindProveedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnFindProveedorActionPerformed(evt);
            }
        });

        lstInsumos.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstInsumos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstInsumosMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lstInsumos);

        BtnNuevo.setText("Nuevo");
        BtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnNuevoActionPerformed(evt);
            }
        });

        BtnClose.setText("Aceptar");
        BtnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnCloseActionPerformed(evt);
            }
        });

        BtnBorrar.setText("Borrar");
        BtnBorrar.setEnabled(false);
        BtnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnBorrarActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Insumos");

        TxtCantidad.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2))
                            .addGap(22, 22, 22)
                            .addComponent(TxtTipoDeInsumo, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(findTipoDeInsumo))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(TxtProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(BtnFindProveedor)))
                    .addComponent(BtnClose, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(TxtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(BtnNuevo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnAceptar)))
                        .addGap(3, 3, 3)
                        .addComponent(BtnBorrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BtnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(TxtProveedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(BtnFindProveedor)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(TxtTipoDeInsumo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(findTipoDeInsumo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(TxtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BtnCancelar)
                            .addComponent(BtnNuevo)
                            .addComponent(BtnAceptar)
                            .addComponent(BtnBorrar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 397, Short.MAX_VALUE)
                        .addComponent(BtnClose))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCancelarActionPerformed
        // TODO add your handling code here:
        this.limpiarCampos();
    }//GEN-LAST:event_BtnCancelarActionPerformed

    private void findTipoDeInsumoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findTipoDeInsumoActionPerformed
        // TODO add your handling code here:
        if(this.unProveedor != null){
            FrmFindTipoInsumo ventana = new FrmFindTipoInsumo(this,true,this.unProveedor);
            ventana.setVisible(true);
        }else{
             JOptionPane.showMessageDialog(this, "Debe Seleccionar un Proveedor");
        }
    }//GEN-LAST:event_findTipoDeInsumoActionPerformed

    private void BtnFindProveedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnFindProveedorActionPerformed
        // TODO add your handling code here:
        FrmFindProveedor ventana = new FrmFindProveedor(this,true,this.unaEmpresa);
        ventana.setVisible(true);
        
    }//GEN-LAST:event_BtnFindProveedorActionPerformed

    private void BtnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnCloseActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BtnCloseActionPerformed

    private void lstInsumosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstInsumosMouseClicked
        // TODO add your handling code here:
        this.unInsumo = (Insumo) this.modelInsumo.getElementAt(this.lstInsumos.getSelectedIndex());
        this.ponerCampos();
    }//GEN-LAST:event_lstInsumosMouseClicked

    private void BtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNuevoActionPerformed
        // TODO add your handling code here:
        this.BtnAceptar.setEnabled(true);
        this.BtnCancelar.setEnabled(true);
        this.BtnNuevo.setEnabled(false);
        this.limpiarCampos();
    }//GEN-LAST:event_BtnNuevoActionPerformed

    private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAceptarActionPerformed
        // TODO add your handling code here:
        if(this.unInsumo == null){
            this.unaEmpresa.nuevoInsumo(this.unTipoDeInsumo,Integer.parseInt(this.TxtCantidad.getText()));
        }else{
            this.unaEmpresa.actualizarInsumo(this.unInsumo,Integer.parseInt(this.TxtCantidad.getText()));
        }
        this.limpiarCampos();
        this.cargarInsumos();
        this.desactivarBotones();
    }//GEN-LAST:event_BtnAceptarActionPerformed

    private void BtnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnBorrarActionPerformed
        // TODO add your handling code here:
        this.unInsumo.setActivo(false);
    }//GEN-LAST:event_BtnBorrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void mains(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmInsumo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmInsumo dialog = new FrmInsumo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JButton BtnBorrar;
    private javax.swing.JButton BtnCancelar;
    private javax.swing.JButton BtnClose;
    private javax.swing.JToggleButton BtnFindProveedor;
    private javax.swing.JButton BtnNuevo;
    private javax.swing.JFormattedTextField TxtCantidad;
    private javax.swing.JTextField TxtProveedor;
    private javax.swing.JTextField TxtTipoDeInsumo;
    private javax.swing.JButton findTipoDeInsumo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lstInsumos;
    // End of variables declaration//GEN-END:variables
}
